package com.yanghui.lingyue.data

import com.icedata.sika.database.Column
import com.icedata.sika.database.Table
import com.yanghui.lingyue.utils.databaseUtils.OperationDatabaseUtils
import com.yanghui.lingyue.utils.operationUtils.OperationCodeUtils
import java.sql.Timestamp

@Table("Operation", "", "operationID")
data class Operation(
    @Column("operationID")
    var operationID: Long,

    @Column("operationCode")
    val instructionCode: Int,

    @Column("userID")
    var userID: Long,

    @Column("operationTime")
    var time: Timestamp = Timestamp(0),

    @Column("groupID")
    var groupID: Long,

    @Column("target")
    var target: Long
) {

    var instruction : String = OperationCodeUtils.getCommandFromCode(instructionCode)

    /**
     * 这个是由外部载入数据库的时候使用的构造函数
     *
     * @param command Command 指令对象，从execute函数中直接获取
     * @constructor
     */
    constructor(command: Command) : this(
        OperationDatabaseUtils.getOperationNum() + 1,
        OperationCodeUtils.getCodeFromCommand(command.instruction),
        command.sender.id,
        Timestamp(command.event.sendTime * 1000.toLong()),
        command.sendGroup?.id ?: command.sender.id,
        command.target
    )

    /**
     * 给数据库方法使用的无参构造函数，不允许用户使用
     * @constructor
     */
    constructor(): this(0,0,0,Timestamp(0),0,0)
}